<?php
class OrdersSeriesController extends AppController {

	var $name = 'OrdersSeries';
	var $uses = array('OrdersSeries','TypeOfService','TypesOfSpaces','TypeOfWork');
	
	var $permissions = array(
		'add' => array('admin','manag'),
		'index' => array('admin','manag'),
		'view' => array('admin','manag','emplo'),
		'edit' => array('admin','manag'),
		'delete' => array('admin','manag'),
		'close' => array('admin','manag'),
		'close_all' => array('admin','manag')
	);

	public $presetVars = array(
		array('field' => 'execution_date', 'type' => 'value'),
		array('field' => 'orders_id', 'type' => 'value'),
		array('field' => 'employee_id', 'type' => 'value'),
		array('field' => 'payment_status', 'type' => 'value'),
		array('field' => 'status', 'type' => 'value')
	);

	public function beforeFilter(){
		parent::beforeFilter();

	}	
	
	function index() {
		// Components closing Series
		$this->Series->close();
				
		$this->OrdersSeries->recursive = 0;		
		App::import('Controller','orders'); 
		$this->set(OrdersController::dictionaries());
		
		App::import('Model','order'); 
		$this->Order = new Order();
		
		$this->Prg->commonProcess();
		$this->paginate['conditions'] = $this->OrdersSeries->parseCriteria($this->passedArgs);
		$ordersSeries = $this->paginate();
		
		$orders = $this->Order->find('all');
		foreach ($orders as $key => $order) {
			$orders_[$order['Order']['id']] = $order['Customer']['name']. ' '.$order['Customer']['surname']. ' ('.$order['Customer']['address1'].'/'.$order['Customer']['address3'].')';
		}
		$orders = $orders_;
		
		$typesOfSpaces = $this->Order->TypesOfSpaces->find('list');
		$employees = $this->OrdersSeries->Employee->find('all');
		foreach ($employees as $key => $employee) {
			$employee_[$employee['Employee']['id']] = $employee['Employee']['name']. ' '.$employee['Employee']['surname']. ' ('.$employee['Employee']['address1'].'/'.$employee['Employee']['address3'].')';
		}
		$employees = $employee_;
		$this->set(compact('ordersSeries', 'employees','orders','paymentStatus','statusOfImplementation'));
	}
	
	public function find() {
		if(!empty($this->data)){
			$this->data['OrdersSeries']['execution_date'] =  $this->EuroDate->convert($this->data['OrdersSeries']['execution_date'],"Y-m-d");
		}
		App::import('Controller','orders'); 
		$this->set(OrdersController::dictionaries());
		App::import('Model','order'); 
		$this->Order = new Order();
		
		$this->Prg->commonProcess();
		$this->paginate['conditions'] = $this->OrdersSeries->parseCriteria($this->passedArgs);
		$ordersSeries = $this->paginate();
		
		$orders = $this->Order->find('all');		
		foreach ($orders as $key => $order) {
			$orders_[$order['Order']['id']] = $order['Customer']['name']. ' '.$order['Customer']['surname']. ' ('.$order['Customer']['address1'].'/'.$order['Customer']['address3'].')';
		}
		$orders = $orders_;
		
		$typesOfSpaces = $this->Order->TypesOfSpaces->find('list');
		$employees = $this->OrdersSeries->Employee->find('all');
		foreach ($employees as $key => $employee) {
			$employee_[$employee['Employee']['id']] = $employee['Employee']['name']. ' '.$employee['Employee']['surname']. ' ('.$employee['Employee']['address1'].'/'.$employee['Employee']['address3'].')';
		}
		$employees = $employee_;
		$this->set(compact('ordersSeries', 'employees','orders','paymentStatus','statusOfImplementation'));
		
		$this->autoRender = false;
		$this->render('index');
	}	

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__("Invalid orders series", true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('ordersSeries', $this->OrdersSeries->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->data['OrdersSeries']['execution_date'] =  $this->EuroDate->convert($this->data['OrdersSeries']['execution_date'],"Y-m-d");
			$this->OrdersSeries->set($this->data);
			if ($this->OrdersSeries->validates($this->data)) {				
				$this->OrdersSeries->create();
				if ($this->OrdersSeries->save($this->data)) {
					$this->Session->setFlash(__("The orders series has been saved", true));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->data['OrdersSeries']['execution_date'] =  $this->EuroDate->convert($this->data['OrdersSeries']['execution_date'],"m-d-Y");
					$this->Session->setFlash(__("The orders series could not be saved. Please, try again.", true));
				}
			}else{
				$this->data['OrdersSeries']['execution_date'] =  $this->EuroDate->convert($this->data['OrdersSeries']['execution_date'],"m-d-Y");
				$this->Session->setFlash(__("The orders series could not be saved. Please, try again.", true));
			}
		}
		
		App::import('Controller','orders'); 
		$this->set(OrdersController::dictionaries());
		
		$typeOfServices = $this->TypeOfService->find('list');
		$typesOfSpaces = $this->TypesOfSpaces->find('list');
		$orders = $this->OrdersSeries->Orders->find('all');
		$typeOfWorks = $this->TypeOfWork->find('list');
		$employees = $this->OrdersSeries->Employee->find('all');
		$this->set(compact('orders', 'employees','typeOfServices','typesOfSpaces','typeOfWorks'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__("Invalid orders series", true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->data['OrdersSeries']['execution_date'] = $this->EuroDate->convert($this->data['OrdersSeries']['execution_date'],"Y-m-d");
			if ($this->OrdersSeries->save($this->data)) {
				$this->Session->setFlash(__("The orders series has been saved", true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__("The orders series could not be saved. Please, try again.", true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->OrdersSeries->read(null, $id);
		}
		
		App::import('Controller','orders'); 
		$this->set(OrdersController::dictionaries());
		
		$typeOfServices = $this->TypeOfService->find('list');
		$typesOfSpaces = $this->TypesOfSpaces->find('list');		
		$typeOfWorks = $this->TypeOfWork->find('list');
		$orders = $this->OrdersSeries->Orders->find('all');
		$employees = $this->OrdersSeries->Employee->find('all');
		$this->set(compact('orders', 'employees','typeOfServices','typesOfSpaces','typeOfWorks'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__("Invalid id for orders series", true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->OrdersSeries->delete($id)) {
			$this->Session->setFlash(__("Orders series deleted", true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__("Orders series was not deleted", true));
		$this->redirect(array('action' => 'index'));
	}


	
		
}
?>